﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using THSERP.BusinessEntities;
using THSERP.BaseProvider;
using System.Drawing;
namespace THSERP.Modules.POS
{
    public class BRPOSItemsGridControl : THSGridControl 
    {
        private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
    
        public override void InitGridControlDataSource()
        {
            //base.InitGridControlDataSource();
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            BindingSource bds = new BindingSource();
            bds.DataSource = entity.BRPOSItemsList;
            this.DataSource = bds;
        }

        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            //Check edit item privilege for gridview_Hieu.nguyen_29/04/2010
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
           // gridView.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gridView_RowStyle);
            //gridView.KeyUp -= new KeyEventHandler(GridView_KeyUp);
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            if(entity.Module.HasPrivilege("EditItem"))
            {
                if (gridView.Columns["BRPOSItemProductQty"] != null)
                    gridView.Columns["BRPOSItemProductQty"].OptionsColumn.AllowEdit = true;
                if (gridView.Columns["BRPOSItemProductUnitPrice"] != null)
                    gridView.Columns["BRPOSItemProductUnitPrice"].OptionsColumn.AllowEdit = true;
                if (gridView.Columns["BRPOSItemProductDesc"] != null)
                    gridView.Columns["BRPOSItemProductDesc"].OptionsColumn.AllowEdit = true;
                if (gridView.Columns["FK_ICProductID"] != null)
                    gridView.Columns["FK_ICProductID"].AppearanceCell.BackColor = Color.WhiteSmoke;
                if (gridView.Columns["BRPOSItemTotalAmount"]!=null)
                    gridView.Columns["BRPOSItemTotalAmount"].AppearanceCell.BackColor = Color.WhiteSmoke;
                if (gridView.Columns["BRPOSItemPrice"] != null)
                    gridView.Columns["BRPOSItemPrice"].AppearanceCell.BackColor = Color.WhiteSmoke;
                gridView.OptionsSelection.EnableAppearanceFocusedRow = false;
            }

            return gridView;
        }
        
        protected override void GridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
             try
                {
                 if(e.RowHandle>=0)
                 {
                    DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                    POSItemsEntity itemEntity = (POSItemsEntity)gridView.GetRow(e.RowHandle);
                    if (itemEntity != null)
                    {
                        if (itemEntity.BRPOSItemProductQty == itemEntity.BRPOSItemQty1)
                        {
                            //e.Appearance.BackColor = Color.Salmon;
                            e.Appearance.ForeColor = Color.Red;
                        }
                        else if (itemEntity.BRPOSItemProductQty > itemEntity.BRPOSItemQty1 && itemEntity.BRPOSItemQty1 > 0)
                        {
                           // e.Appearance.BackColor = Color.Salmon;
                            e.Appearance.ForeColor = Color.DarkBlue;

                        }
                        else
                        {
                            //e.Appearance.BackColor = Color.Salmon;
                            e.Appearance.ForeColor = Color.Olive;
                        }
                    }
                }
             }
            catch (Exception ex)
            {

            }
        }

        protected override void GridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
            {
            POSEntities entity = (POSEntities)(this.Screen.Module as BaseModuleERP).CurrentModuleEntity;
            if (((BRPOSsInfo)entity.MainObject).BRPOSStatusCombo == POSModule.cstPOSStatusComplete)
                return;
            base.GridView_CellValueChanged(sender, e);

           // POSEntities entity = (POSEntities)(this.Screen.Module as BaseModuleERP).CurrentModuleEntity;
            if (entity.BRPOSItemsList.CurrentIndex >= 0)
            {
                POSItemsEntity itemEntity = (POSItemsEntity)entity.BRPOSItemsList[entity.BRPOSItemsList.CurrentIndex];
                if (itemEntity.BRPOSItemProductQty < itemEntity.BRPOSItemQty1)
                {
                    DialogResult dlg = MessageBox.Show("Món đã được In chế biến không được sửa. Có phải bạn muốn trả món?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dlg == DialogResult.Yes)
                        ((POSModule)this.Screen.Module).ActionReturnItems();
                    else
                    {
                        BRPOSItemsInfo objBRPOSItmsInfo = (BRPOSItemsInfo)entity.BRPOSItemsList[entity.BRPOSItemsList.CurrentIndex].SetToBusinessObject(POSEntities.strBRPOSItemsObjectName);
                        ((BRPOSItemsInfo)entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName]).BRPOSItemProductQty = ((BRPOSItemsInfo)entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName]).BRPOSItemProductQtyOld;
                        (entity.Module as POSModule).ChangeItemFromPOSItemsList();
                    }
                }
                else
                {
                    entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName] = entity.BRPOSItemsList[entity.BRPOSItemsList.CurrentIndex].SetToBusinessObject(POSEntities.strBRPOSItemsObjectName);
                    //bat trường hợp thay đổi số lương khi phiếu đã in chế biến
                    (entity.Module as POSModule).ChangeItemFromPOSItemsList();
                }
            }
        }
       
        //protected override string GetLookupTableDisplayColumn(string strLookupTableName)
        //{
        //    switch (strLookupTableName)
        //    {
        //        case "ICProducts":
        //            return "ICProductName";
        //        case"GELocations":
        //            return "GELocationName";
        //        default:
        //            return base.GetLookupTableDisplayColumn(strLookupTableName);
        //    }
        //}
       
    }

    }
